package com.cqndt.site.service.web.impl;


import com.cqndt.site.dao.*;
import com.cqndt.site.entity.*;
import com.cqndt.site.service.back.TabJournalRecordService;
import com.cqndt.site.service.web.ITabJournalRecordService;
import com.cqndt.site.util.Result;
import com.cqndt.site.vo.ConditionVo;
import com.cqndt.site.vo.RecordDetailVo;
import javafx.scene.control.Tab;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.List;

@Service
public class ITabJournalRecordServiceImpl implements ITabJournalRecordService {

    @Autowired
    private TabJournalRecordMapper recordMapper;
    @Autowired
    private TabGuideMapper guideMapper;
    @Autowired
    private TabContryTrendMapper contryTrendMapper;
    @Autowired
    private TabProvinceTrendMapper provinceTrendMapper;
    @Autowired
    private TabIndustryTrendMapper industryTrendMapper;
    @Autowired
    private TabTenderingMapper tenderingMapper;
    @Autowired
    private TabWinbidMapper winbidMapper;
    @Autowired
    private TabCompeteAnalysisMapper competeAnalysisMapper;
    @Autowired
    private TabAttachmentMapper attachmentMapper;

    @Value("${file-ip}")
    private String fileIp;

    @Value("${front-url}")
    private String frontUrl;

    @Override
    public Result getRecordByNum(Integer periodNum) {
        Result result = new Result();
        TabJournalRecord record = new TabJournalRecord();
        if(null == periodNum){
           //如果期数为空则查询最新一期已发布期刊
            Integer max = recordMapper.getMaxPeriodNum(2);
            record = recordMapper.getRecordByPeriodNum(max);
        }else{
            record = recordMapper.getRecordByPeriodNum(periodNum);
        }

        //获取本期导读
        TabGuide tabGuide = guideMapper.selectByRecordId(record.getId());
        //获取国家行业动态
        TabContryTrend tabContryTrend = contryTrendMapper.selectByRecordId(record.getId());
        if(null !=tabContryTrend && null!= tabContryTrend.getImgId() && !("").equals(tabContryTrend.getImgId())){
            //图片路径
            String[] imgIds = tabContryTrend.getImgId().split(",");
            List<String> imgUrl = attachmentMapper.getUrlByIds(imgIds,fileIp);
            tabContryTrend.setImgUrl(imgUrl);
        }
        //获取各省行业动态
        TabProvinceTrend tabProvinceTrend = provinceTrendMapper.selectByRecordId(record.getId());
        //获取行业学会动态
        List<TabIndustryTrend> tabIndustryTrend = industryTrendMapper.selectByRecordId(record.getId());
        //获取招标信息
        TabTendering tabTendering = tenderingMapper.selectByRecordId(record.getId());
        //获取招标信息
        TabWinbid tabWinbid = winbidMapper.selectByRecordId(record.getId());
        //获取竞争者分析
        List<TabCompeteAnalysis> tabCompeteAnalysis = competeAnalysisMapper.selectByRecordId(record.getId());

        RecordDetailVo vo = new RecordDetailVo();
        vo.setPeriodNum(record.getPeriodNum());
        vo.setLinkUrl(frontUrl+ File.separator+record.getPeriodNum()+File.separator+ record.getLinkUrl());
        vo.setTabCompeteAnalysis(tabCompeteAnalysis);
        vo.setTabContryTrend(tabContryTrend);
        vo.setTabGuide(tabGuide);
        vo.setTabIndustryTrend(tabIndustryTrend);
        vo.setTabProvinceTrend(tabProvinceTrend);
        vo.setTabTendering(tabTendering);
        vo.setTabWinbid(tabWinbid);
        result.setData(vo);
        return result;
    }

}
